<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="head-title2">
        <div class='title'>
          {{$t('achievements.title1')}}
        </div>
        <div class="info">
          <el-tooltip placement="left">
            <div slot="content">
              {{define}}
            </div>
            <div class="btns"></div>
          </el-tooltip>
        </div>
      </div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 日期 -->
          <div class="searchItem searchItem-date">
            <span>{{$t('overdueData.time')}}</span>
            <el-date-picker
              size="small"
              v-model="chooseDate"
              type="daterange"
              :range-separator="$t('btn.go')"
              :start-placeholder="$t('rule.beginDate')"
              :end-placeholder="$t('rule.endDate')"
              value-format="yyyy-MM-dd"
              @change="dateChangeTime"
            ></el-date-picker>
          </div>
          <!-- 催收公司 -->
          <div class="searchItem">
            <span>{{$t('CollStatisReport.CollectCompany')}}</span>
            <el-select v-model="listQuery.companyId" size="small">
              <el-option
                v-for="(item, index) in collCompanyArr"
                :key="index"
                :label="item.companyName"
                :value="item.ids"
              ></el-option>
            </el-select>
          </div>
          <div class="menu-title-btn">
            <button type="button" @click="search">{{$t('btn.query')}}</button>
            <button type="button" @click="reset">{{$t('btn.reset')}}</button>
            <button
              type="button"
              @click="handleDownload"
              :disabled="expDis"
              v-if="collStatisReport_btn_export"
            >{{$t('btn.export')}}</button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('DailyStatistics.ListDetails')}}</div>
      <el-table
        :sum-text="$t('ApprReport.total')"
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        size="mini"
      >
        <!-- 逾期阶段 -->
        <el-table-column
          align="center"
          header-align="center"
          prop="agingName"
          :label="$t('CollStatisReport.OverdueStage')"
        ></el-table-column>
        <!-- 上期留存案件 -->
        <el-table-column
          align="center"
          header-align="center"
          prop="retainedCaseNo"
          :label="$t('CollStatisReport.PreRetentionCase')"
        ></el-table-column>
        <!-- 新增案件 -->
        <el-table-column
          align="center"
          header-align="center"
          prop="addCaseNo"
          :label="$t('CollStatisReport.AddCase')"
        ></el-table-column>
        <!-- 成功案件 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="successCaseNo"
          :label="$t('CollStatisReport.SuccessCase')"
        ></el-table-column>
        <!-- 失败案件 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="failedCaseNo"
          :label="$t('CollStatisReport.FailureCase')"
        ></el-table-column>
        <!-- 需处理案件总数 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="handleCaseNo"
          :label="$t('CollStatisReport.TodoCaseTotal')"
        ></el-table-column>
        <!-- 剩余处理案件数 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="remainingCaseNo"
          :label="$t('CollStatisReport.SurplusCase')"
        ></el-table-column>
        <!-- 催回率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="recallRate"
          :label="$t('CollStatisReport.RecoveryRate')"
        ></el-table-column>
        <!-- 失败率 -->
        <el-table-column
          header-align="center"
          align="center"
          prop="failedRate"
          :label="$t('CollStatisReport.FailureRate')"
        ></el-table-column>
      </el-table>
      <!-- <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div> -->
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { padLeftZero, toPercent } from "utils/index";
import { getCollectCompanyDropDown, collectionStatistics, collectionExport } from "api/reportForm/CollReport/CollStatisReport/index";
export default {
  data() {
    return {
      define: '',
      chooseDate: [new Date(), new Date()],
      collStatisReport_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 5,
        page: 1,
        startDate: "",
        endDate: "",
        companyId: ""
      },
      tableData: [{recallRate: 0.2312}],
      collCompanyArr: [
        {
          ids: "",
          companyName: this.$t("rule.all")
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "CollStatisReportPage", 'getReportDefine'])
  },
  created() {
    if (this.CollStatisReportPage.has(this.$route.path)) {
      this.listQuery = this.CollStatisReportPage.get(this.$route.path);
    }
    // 催收公司
    getCollectCompanyDropDown().then(res => {
      if (res.status === 200) {
        this.collCompanyArr = this.collCompanyArr.concat(res.data);
      }
    })
    this.getTime();
    this.getList();
    this.collStatisReport_btn_export = this.elements["collStatisReport:btn_export"];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'COLLECTION_REPORT') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    // 日期
    dateChangeTime(val) {
      if (val === undefined || val === null || val === "") {
        this.listQuery.startDate = "";
        this.listQuery.endDate = "";
      } else {
        this.listQuery.startDate = val[0];
        this.listQuery.endDate = val[1];
      }
    },
    getTime() {
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      const dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期
      const y = dd.getFullYear();
      const m = padLeftZero(dd.getMonth() + 1, 2);
      const d = padLeftZero(dd.getDate(), 2);
      return y + "-" + m + "-" + d;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      collectionExport(this.listQuery).then(res => {
        res.data.forEach(item => {
          if (item.recallRate || item.failedRate) {
            item.recallRate = toPercent(item.recallRate);
            item.failedRate = toPercent(item.failedRate);
          }
        });
        require.ensure([], () => {
          const { export_json_to_excel } = require("@/vendor/Export2Excel");
          const tHeader = [
            this.$t("CollStatisReport.OverdueStage"),
            this.$t("CollStatisReport.PreRetentionCase"),
            this.$t("CollStatisReport.AddCase"),
            this.$t("CollStatisReport.SuccessCase"),
            this.$t("CollStatisReport.FailureCase"),
            this.$t("CollStatisReport.TodoCaseTotal"),
            this.$t("CollStatisReport.SurplusCase"),
            this.$t("CollStatisReport.RecoveryRate"),
            this.$t("CollStatisReport.FailureRate")
          ];
          const filterVal = [
            "agingName",
            "retainedCaseNo",
            "addCaseNo",
            "successCaseNo",
            "failedCaseNo",
            "handleCaseNo",
            "remainingCaseNo",
            "recallRate",
            "failedRate"
          ];
          const list = res.data;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(
            tHeader,
            data,
            this.$t("route.CollStatisReport")
          );
          this.listLoading = false;
        });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    getList() {
      if (this.listQuery.startDate === '' || this.listQuery.endDate === ''
      || this.listQuery.startDate === null || this.listQuery.endDate === null
      || this.listQuery.startDate === undefined || this.listQuery.endDate === undefined) {
        this.$message({
          showClose: true,
          type: "warning",
          message: this.$t('CollStatisReport.ChooseDate')
        });
      } else {
        this.listLoading = true;
        this.$store.dispatch("saveCollStatisReportPage", {
          path: this.$route.path,
          listQuery: this.listQuery
        });
        collectionStatistics(this.listQuery).then(res => {
          res.data.forEach((item, index) => {
            item.index =
              index + (this.listQuery.page - 1) * this.listQuery.limit + 1;
            if (item.recallRate || item.failedRate) {
              item.recallRate = toPercent(item.recallRate);
              item.failedRate = toPercent(item.failedRate);
            }
          });
          this.expDis = false;
          this.listLoading = false;
          this.tableData = res.data;
          this.total = Number(res.data.total);
        });
      }
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.chooseDate = [new Date(), new Date()];
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
      this.listQuery.companyId = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 220px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>